import java.util.*;


public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     *
     * 
     * @param n int整型 
     * @return string字符串ArrayList
     */
    public ArrayList<String> generateParenthesis (int n) {
        // write code here
        ArrayList<String> res = new ArrayList<>();
        String s = new String();
        fun(res,s,0,0,n);
        return res;
    }
    public void fun(ArrayList<String> res,String s,int left,int right,int n) {
        if(left == n && right == n) {
            res.add(new String(s));
        }
        if(left < n) {
            
            fun(res,s+"(",left+1,right,n);
        }
        if(right < left) {
       
            fun(res,s+")",left,right+1,n);
        }
    }
}